package ranks.base;

import mapred.util.Configuration;
import ranks.Customize;
import ranks.Customize.DataType;

public class Data extends Configuration<Customize.DataType, String> {

    public Data() {
        super(null, "=", ",");
    }
    
	public Data(String s) {
		this();
		
		DataType[] dataTypes = DataType.values();
		String[] values = s.split(",");
		for (int i = 0; i < values.length; ++i) {
		    this.add(dataTypes[i], values[i]);
		}
	}

	@Override
	protected DataType decodeKey(String strKey) {
	    throw new RuntimeException("Not support decodeKey in Data class.");
	}

	@Override
	protected String decodeValue(String strValue) {
        throw new RuntimeException("Not support decodeValue in Data class.");
	}

	@Override
	protected boolean isValidKey(DataType key) {
        return (key != null);
	}

	@Override
	protected String mergeValue(DataType key, String prev, String cur) {
		return key.mergeValue(prev, cur);
	}

	public String get(DataType key) {
	    String ret = super.get(key);
	    if (ret == null || ret.isEmpty()) {
	        return "0";
	    }
	    return ret;
	}
	
	@SuppressWarnings("unchecked")
    public <T> T getValue(DataType key) {
	    return (T)key.parse(this.get(key));
	}
	
	public String toString() {
	    StringBuilder sb = new StringBuilder();
	    for (DataType dt : DataType.values()) {
	        sb.append(",");
	        sb.append(this.get(dt));
	    }
	    return sb.substring(1);
	}
	
	public void set(DataType key, Object value) {
	    this.remove(key);
	    this.add(key, value.toString());
	}

	public void add(Data... others) {
	    super.add(others);
	}
	
}
